from django.http import Http404, HttpResponseRedirect
from yearbook.models import Form, Student, CommentOnStudent
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse

def index(request):
    return render_to_response('index.html')

def formindex(request):
    form_list = Form.objects.all()
    return render_to_response('forms/index.html', {'form_list': form_list})

def detail(request, form_id):
    f = get_object_or_404(Form, id=form_id)
    s = Student.objects.all()[:5]
    return render_to_response('forms/detail.html', {'form': f, 'students': s})

def comment(request, form_id):
    f = get_object_or_404(Form, id=form_id)
    student_id = request.POST['student']
    s = Student.objects.get(id=student_id)
    c = request.POST['comment']
    obj = CommentOnStudent(comment=c,student=s,form=f)
    obj.save()
    return HttpResponseRedirect(reverse('yearbook.views.detail', args=(f.id,)))

